package com.xwh.shop.orders.service.controller;

import com.xwh.shop.common.Result;
import com.xwh.shop.orders.model.dto.CartDTO;
import com.xwh.shop.orders.model.entity.Cart;
import com.xwh.shop.orders.service.handlers.BaseContextHandler;
import com.xwh.shop.orders.service.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwh
 * @since 2025-07-17
 */
@Slf4j
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车管理", description = "购物车相关接口")
public class CartController {
    @Resource
    private CartService cartService;
    @PostMapping("add")
    @Operation(summary = "新增购物车项")
    public Result<Object> addCart(@RequestBody CartDTO cartDTO){
        log.info("添加购物车：{}", cartDTO);
        cartService.addCart(cartDTO);
        return Result.success();
    }

    //todo 07-17 实现购物车条目的减少功能
    @Operation(summary = "减少购物车条目")
    @PostMapping("reduce")
    public Result<Object> reduceItemCart(@RequestBody CartDTO cartDTO){
        boolean res = cartService.reduceItemCart(cartDTO);
        log.info("reduceItemCart res:{}",res);
        return Result.success();
    }

    @Operation(summary = "清空购物车")
    @DeleteMapping("delete")
    public Result<Object> deleteCart(){
        Long memberId = BaseContextHandler.get();
        cartService.deleteCart(memberId);
        return Result.success();
    }

    @Operation(summary = "修改购物车条目")
    @PostMapping("update")
    public Result<Cart> updateCart(@RequestBody CartDTO cartDTO){
        Cart cart = cartService.updateCart(cartDTO);
        return Result.success(cart);
    }

    @Operation(summary = "查询购物车")
    @GetMapping("getCart")
    public void getCart(){
        cartService.getCart();
    }

}
